﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Reflection;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows.Shapes;
using CrazyWorship1.Class_Files.PropertyHandler;

namespace CrazyWorship1.Transitions
{
    public class BaseTransition
    {

        #region Properties

        public void SetProperty(string name, string value)
        {
            Type type = this.GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo propertyInfo in properties)
            {
                if (propertyInfo.IsDefined(typeof(DataAttribute), true) && propertyInfo.CanWrite)
                {
                    object[] fields = propertyInfo.GetCustomAttributes(typeof(DataAttribute), true);
                    foreach (DataAttribute dataAttribute in fields)
                    {
                        if (dataAttribute.FieldName == name)
                        {
                            propertyInfo.SetValue(this, value, null);
                        }
                    }

                }
            }
        }
        public virtual void SetProperties()
        {
        }
        #endregion 

        #region Public Properties


        ICreator _creator;
        public ICreator Creator
        {
            get
            {
                return _creator;
            }

        }
        private State currentState;
        protected State CurrentState
        {
            get
            {
                return currentState;
            }
        }
        
        private Dictionary<Guid, Visual> textBlocks;
        protected Dictionary<Guid, Visual> TextBlocks
        {
            get
            {
                return textBlocks;
            }
        }
        private Dictionary<Guid, GeometryModel3D> textPanes;
        protected Dictionary<Guid, GeometryModel3D> TextPanes
        {
            get
            {
                return textPanes;
            }

        }
        public GeometryModel3D PreviousLyricTextPane
        {
            get
            {
                return TextPanes[CurrentState.PreviousLyric.LyricID];
            }
        }
        public Visual PreviousLyricTextBlock
        {
            get
            {
                return textBlocks[CurrentState.PreviousLyric.LyricID];
            }
        }
        public GeometryModel3D CurrentLyricTextPane
        {
            get
            {
                return TextPanes[CurrentState.CurrentLyric.LyricID];
            }
        }
        public Visual CurrentLyricTextBlock
        {
            get
            {
                return textBlocks[CurrentState.CurrentLyric.LyricID];
            }
        }
#endregion

        public Transform3DGroup GetTransitionGroup(GeometryModel3D m, TransitionGroup g)
        {
            if (g == TransitionGroup.Effect)
                return (Transform3DGroup)((Transform3DGroup)m.Transform).Children[2];
            else if (g == TransitionGroup.Transition)
                return (Transform3DGroup)((Transform3DGroup)m.Transform).Children[1];
            else if (g == TransitionGroup.Static)
                return (Transform3DGroup)((Transform3DGroup)m.Transform).Children[0];
            throw new Exception("Invalid Transition Group");
        }
      
        public void PrepareTransition(ICreator creator)
        {
            _creator = creator;
            currentState = creator.CurrentState;
            textBlocks = creator.TextBlocks;// textblocks;
            textPanes = creator.TextPanes;// textpanes;
            SetupTransition();
            SetProperties();
        }

        public void StartAnimation(Visual v, DependencyProperty p, DoubleAnimation f)
        {
            if (v is TextBlock)
                ((TextBlock)v).BeginAnimation(p, f);
            else if (v is Path)
                if (p == TextBlock.OpacityProperty)
                    ((Path)v).BeginAnimation(Path.OpacityProperty, f);


        }
        public Dictionary<string, PropertyHandlerControl> PropertyHandlers
        {
            get
            {
                Dictionary<string, PropertyHandlerControl> dict = new Dictionary<string, PropertyHandlerControl>();

                Type t = this.GetType();
                PropertyInfo[] p = t.GetProperties();
                //  ArrayList properties = new ArrayList();

                foreach (PropertyInfo pi in p)
                {
                    if (pi.IsDefined(typeof(DataAttribute), true) && pi.CanWrite)
                    {
                        object[] fields = pi.GetCustomAttributes(typeof(DataAttribute), true);
                        foreach (DataAttribute d in fields)
                        {

                            dict.Add(d.FieldName, d.Handler);


                        }

                    }
                }
                return dict;
            }
        }
       
        public virtual void SetupTransition()
        {
        }
       
      
      
    }
}
